#include "gamecontrol.h"

GameControl* GameControl :: instance = nullptr;
GameControl::GameControl(QWidget* parent) : QObject(parent){

    //初始化数据
    vyblue = 0;
    vy1blue = 4;
    gravity = 0.5;
    isJumpingblue = false;
    mMoveSpeed = 4.5;

    vyred = 0;
    vy1red = 4;
    isJumpingRed = false;

    //添加平台
    //顶层
    addPlatform(40,10,1250,40);
    //第一层
    addPlatform(280,150,150,30);
    addPlatform(380,180,100,130);
    addPlatform(480,210,70,100);
    addPlatform(550,210,370,40);
    addPlatform(920,180,110,70);
    addPlatform(1030,210,250,40);
    //第二层
    addPlatform(40,250,161,170);
    addPlatform(210,380,440,40);
    addPlatform(650,330,300,90);
    addPlatform(980,380,190,70);
    //第三层
    addPlatform(170,520,480,40);
    addPlatform(650,520,70,60);
    addPlatform(720,550,371,40);
    addPlatform(1100,550,140,70);
    addPlatform(1230,550,71,110);
    //第四层
    addPlatform(40,690,540,40);
    addPlatform(580,690,60,100);
    addPlatform(640,750,151,40);
    addPlatform(790,790,101,30);
    addPlatform(990,750,171,40);
    //四层半
    addPlatform(40,820,400,30);
    //第五层
    addPlatform(1200,860,141,90);
    addPlatform(40,959,581,31);
    addPlatform(790,959,111,31);
    addPlatform(1050,959,111,31);
    //小平台
    addPlatform(50,615,100,30);

}

void GameControl::GameInit()
{
    //设置视图的父亲为窗口
    mGameView.setParent(Widget::widget);

    LoadStartScene();//开始场景初始化

    //定时器初始化
    mPlayerMoveTimer = new QTimer(this);
    mFormTimer = new QTimer(this);

    // //绑定定时器处理函数
    connect(mPlayerMoveTimer,&QTimer::timeout,this,&GameControl::move);
    connect(mPlayerMoveTimer,&QTimer::timeout,this,&GameControl::updatePosition);
    connect(mPlayerMoveTimer,&QTimer::timeout,this,&GameControl::changePixmapBlue);
    connect(mPlayerMoveTimer,&QTimer::timeout,this,&GameControl::changePixmapRed);
    connect(mPlayerMoveTimer,&QTimer::timeout,this,&GameControl::downCollisions);
    connect(mFormTimer,&QTimer::timeout,this,&GameControl::detectCollisions);
    connect(mPlayerMoveTimer,&QTimer::timeout,this,&GameControl::GameOver);

}
void GameControl::LoadStartScene()
{
    mStartScene.setSceneRect(QRect(0,0,1347,1009));
    mStartScene.addPixmap(QPixmap(":/1/Image/BackgroundStart.png"));
    auto startbtn = new QToolButton;
    startbtn->setAutoRaise(true);
    startbtn->setIcon(QIcon(":/1/Image/ButtonStart.png"));
    startbtn->setIconSize(QSize(353,46));
    startbtn->move(470,500);

    //开始游戏点击
    connect(startbtn,&QToolButton::clicked,this,[this](){

    //加载游戏场景
    this->LoadGameScene();
    //开始游戏
    this->GameStart();

    });
     mStartScene.addWidget(startbtn);

    //设置当前场景为开始场景
    mGameView.setScene(&mStartScene);
    mGameView.show();

}
void GameControl::LoadGameScene()
{

    mGameScene.setSceneRect(QRect(0,0,1347,1009));
    //设置坐标
    mGameScene.setSceneRect(QRect(0,0,1347,1009));
    mWaterRed.setPos(620,937);
    mWaterBlue.setPos(890,907);
    mWaterGreen.setPos(840,715);
    mStrategyUpdown.setPos(30,590);
    mDoorBlue.setPos(1220,140);
    mDiamondBlue.setPos(665,850);

    //添加图片
    mBackground.setPixmap(QPixmap(":/1/Image/Background.png"));
    mWaterRed.setPixmap(QPixmap(":/1/Image/WaterRed.png"));
    mWaterBlue.setPixmap(QPixmap(":/1/Image/WaterBlue.png"));
    mWaterGreen.setPixmap(QPixmap(":/1/Image/WaterGreen.png"));
    mStrategyUpdown.setPixmap(QPixmap(":/1/Image/StrategyUpdown.png"));
    mDoorBlue.setPixmap(QPixmap(":/1/Image/DoorBlue4.png"));
    mDiamondBlue.setPixmap(QPixmap(":/1/Image/DiamondBlue.png"));

    //图片元素添加到场景
    mGameScene.addItem(&mBackground);
    mGameScene.addItem(&mWaterRed);
    mGameScene.addItem(&mWaterBlue);
    mGameScene.addItem(&mWaterGreen);
    mGameScene.addItem(&mStrategyUpdown);
    mGameScene.addItem(&mDoorBlue);
    mGameScene.addItem(&mDiamondBlue);
    mGameScene.addItem(&mManRed);
    mGameScene.addItem(&mManBlue);

    //设置当前场景为游戏场景
    mGameView.setScene(&mGameScene);
    mGameView.show();

}
//游戏开始
void GameControl::GameStart()
{
    //开始定时器
    mPlayerMoveTimer->start(playerMoveTimer);
    mFormTimer->start(platformTimer);
}
//游戏结束
void GameControl::GameOver()
{

    this->setParent(Widget::widget);
    QRectF mManBlueRect = mManBlue.boundingRect().translated(mManBlue.pos());
    mManBlueRect = QRectF(mManBlue.x(),mManBlue.y(),25,25);
    QRectF mDoorBlueRect = mDoorBlue.boundingRect().translated(mDoorBlue.pos());
    if(mManBlueRect.intersects(mDoorBlueRect))
    {
       mSuccessScene.addPixmap(QPixmap(":/1/Image/Success.png"));
       mGameView.setScene(&mSuccessScene);
       mPlayerMoveTimer->stop();
      mFormTimer->stop();
      mGameView.show();
    }

}
//添加平台到场景
void GameControl::addPlatform(double x,double y,int width,int height)
{
    mGameScene.addRect(x,y,width,height);
    platforms.append(QRectF(x,y,width,height));
}
//人物移动
void GameControl::move()
{
    for(int i=0;i< GameControl::Instance()->mKeyList.size();i++)
    {
        switch( GameControl::Instance()->mKeyList[i])
        {
        case Qt::Key_W:
        {
            mManBlue.setPixmap(QPixmap(":/1/Image/ManBlueUp.png"));
            jumpblue();
            break;
        }
        case Qt::Key_S:
        {
            mManBlue.setPixmap(QPixmap(":/1/Image/ManBlueDown.png"));
            downblue();
            break;
        }
        case Qt::Key_A:
        {
            mManBlue.setPixmap(QPixmap(":/1/Image/ManBlueLeft.png"));
            mManBlue.moveBy(-1 * mMoveSpeed,0);
            break;
        }
        case Qt::Key_D:
        {
            mManBlue.setPixmap(QPixmap(":/1/Image/ManBlueRight.png"));
            mManBlue.moveBy(1*mMoveSpeed,0);
            break;
        }
        case Qt::Key_5:
        {
            mManRed.setPixmap((QPixmap(":/1/Image/ManRedUp.png")));
            jumpred();
            break;
        }
        case Qt::Key_1://左
        {
            mManRed.setPixmap(QPixmap(":/1/Image/ManRedLeft.png"));
            mManRed.moveBy(-1 * mMoveSpeed,0);
            break;
        }
        case Qt::Key_2:
        {
            mManRed.setPixmap((QPixmap(":/1/Image/ManRedDown.png")));
            downred();
            break;
        }
        case Qt::Key_3://右
        {
            mManRed.setPixmap(QPixmap(":/1/Image/ManRedRight.png"));
            mManRed.moveBy(1*mMoveSpeed,0);
            break;
        }

        }
    }
    //检测碰到边界
    boundaryJudge();
    //检测碰到水
    watergreenJudge();
    waterRedJudge();

}
//更换图片
void GameControl::changePixmapBlue()
{
    if( GameControl::Instance()->mKeyList.size() == 0)
    {
        mManBlue.setPixmap((QPixmap(":/1/Image/ManBlueStand.png")));
    }
}
void GameControl::changePixmapRed()
{
    if(GameControl::Instance()->mKeyList.size() == 0)
    {
        mManRed.setPixmap((QPixmap(":/1/Image/ManRedStand.png")));
    }
}
void GameControl::waterRedJudge()
{
    if(mManBlue.x()>590 && mManBlue.x()<700 && mManBlue.y() >875 && mManBlue.y()<880)//620,937,191,57
    {
        mManBlue.setPos(20,879);
    }
}
void GameControl::updatePosition()
{
    if(isJumpingblue)
    {
        vyblue -= gravity;
        mManBlue.setY(mManBlue.y()-vyblue);

    }

    if(mManBlue.y()<0)
    {
        mManBlue.setY(0);
        vyblue = 0;
        isJumpingblue = false;
    }

    if(mManBlue.y()>ScreenHeight-mManBlue.pixmap().height()-30 )

    {
        mManBlue.setY(ScreenHeight-mManBlue.pixmap().height()-30);
        vyblue = 0;
        isJumpingblue = false;
    }

    if(isJumpingRed)
    {
        vyred -= gravity;
        mManRed.setY(mManRed.y()-vyred);

    }

    if(mManRed.y()<0)
    {
        mManRed.setY(0);
        vyred = 0;
        isJumpingRed = false;
    }

    if(mManRed.y()>ScreenHeight-mManRed.pixmap().height()-30 )

    {
        mManRed.setY(ScreenHeight-mManRed.pixmap().height()-30);

        vyred = 0;
        isJumpingRed = false;
    }

}
void GameControl::jumpblue()
{
    if(!isJumpingblue)
    {
        vyblue = 12;
        isJumpingblue = true;
    }
}
void GameControl::jumpred()
{
    if(!isJumpingRed)
    {
        vyred = 12;
        isJumpingRed = true;
    }
}
//边界判断(player)
void GameControl::boundaryJudge()
{
    if(mManBlue.x()<20)
    {
        mManBlue.setX(20);
    }

    if(mManBlue.x()>ScreenWidth-mManBlue.pixmap().width())

    {
       mManBlue.setX(ScreenWidth-mManBlue.pixmap().width());
    }
    if(mManBlue.y()>ScreenHeight-mManBlue.pixmap().height()-30)

    {
        mManBlue.setY(ScreenHeight-mManBlue.pixmap().height()-30);

    }
    //单独检测左右两个凸出部分
    if(mManRed.x()>1131&&mManRed.y()>877)
    {
        mManRed.setPos(1130,879);

    }
    if(mManRed.x()<20)
    {
        mManRed.setX(20);
    }

    if(mManRed.x()>ScreenWidth-mManRed.pixmap().width())

    {
        mManRed.setX(ScreenWidth-mManRed.pixmap().width());
    }
    if(mManRed.y()>ScreenHeight-mManRed.pixmap().height()-30)

    {
        mManRed.setY(ScreenHeight-mManRed.pixmap().height()-30);

    }
    //单独检测左右两个凸出部分
    if(mManRed.x()>1131&&mManRed.y()>877)
    {
        mManRed.setPos(1130,879);

    }
}
//检测是否在平台上 人物下部分碰到平台 站在平台上(player)
void GameControl::detectCollisions()
{
    bool onPlatformblue = false;
    bool onPlatformred = false;
    QRectF mManBlueRect1 = mManBlue.boundingRect().translated(mManBlue.pos());
    mManBlueRect1 = QRectF(mManBlueRect1.x()+30,mManBlueRect1.y()+80,25,1);
    QRectF mManRedRect1 = mManRed.boundingRect().translated(mManRed.pos());
    mManRedRect1 = QRectF(mManRedRect1.x()+30,mManRedRect1.y()+80,25,1);
    for(int i=0 ;i<platforms.size();i++)
    {
        if(mManBlueRect1.intersects(platforms[i]))
        {
            mManBlue.setY(platforms[i].y()-mManBlue.boundingRect().height()+20);
            vyblue = 0;
            isJumpingblue = false;
            onPlatformblue = true;
            break;
        }
        if(mManRedRect1.intersects(platforms[i]))
        {
            mManRed.setY(platforms[i].y()-mManRed.boundingRect().height()+20);
            vyred = 0;
            isJumpingRed = false;
            onPlatformred = true;
            break;
        }
    }
    if(!onPlatformblue)
    {
        downblue();//检测人物是否在平台上 不在就掉下来
    }
    if(!onPlatformred)
    {
        downred();//检测人物是否在平台上 不在就掉下来
    }
}
//检测人物上部分碰到平台 反弹
void GameControl::downCollisions()
{
    QRectF mManBlueRect2 = mManBlue.boundingRect().translated(mManBlue.pos());
    mManBlueRect2 = QRectF(mManBlueRect2.x()+20,mManBlueRect2.y()+25,50,1);
    QRectF mManRedRect2 = mManRed.boundingRect().translated(mManRed.pos());
    mManRedRect2 = QRectF(mManRedRect2.x()+20,mManRedRect2.y()+25,50,1);
    for(int i=0;i<platforms.size();i++)
    {
        if(mManBlueRect2.intersects(platforms[i]))
        {
            vyblue = -vyblue;
            break;
        }
        if(mManRedRect2.intersects(platforms[i]))
        {
            vyred = -vyred;
            break;
        }
    }

}
//绿水判断(player)
void GameControl::watergreenJudge()
{
    if(mManBlue.x()>840 && mManBlue.x()<980 && mManBlue.y()>725 && mManBlue.y()<735)//绿水(840,715,140,73
    {
        mManBlue.setPos(20,879);
    }
    if(mManRed.x()>840 && mManRed.x()<980 && mManRed.y()>725 && mManRed.y()<735)//绿水(840,715,140,73
    {
        mManRed.setPos(70,879);
    }

}

//模拟下落
void GameControl::downblue()
{
    vyblue += gravity;
    mManBlue.setY(mManBlue.y()+vyblue);

}
void GameControl::downred()
{

    vyred += gravity ;
    mManRed.setY((mManRed.y()+vyred));
}




//写钻石，
